package com.lagou.niancheng.lagouserviceuser.service.impl;

import com.lagou.niancheng.lagouserviceuser.common.entity.TokenDO;
import com.lagou.niancheng.lagouserviceuser.dao.AccessTokenDAO;
import com.lagou.niancheng.lagouserviceuser.service.TokenService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * com.lagou.niancheng.lagouserviceuser.service.impl.TokenServiceImpl
 *
 * @author 大犬
 * @date 2020/6/28
 */
@Service
public class TokenServiceImpl implements TokenService {

    @Resource
    private AccessTokenDAO accessTokenDAO;

    @Override
    public String generateToken(String email) {
        String accessToken = UUID.randomUUID().toString().replaceAll("-", "");
        TokenDO tokenDO = new TokenDO();
        tokenDO.setToken(accessToken);
        tokenDO.setEmail(email);
        accessTokenDAO.insert(tokenDO);
        return accessToken;
    }

    @Override
    public boolean checkToken(String token) {
        TokenDO tokenDO = accessTokenDAO.getByToken(token);
        if (tokenDO == null) {
            return false;
        }
        return true;
    }

    @Override
    public String getEmail(String token) {
        TokenDO tokenDO = accessTokenDAO.getByToken(token);
        if (tokenDO == null) {
            return null;
        }
        return tokenDO.getEmail();
    }


}
